﻿
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO.Compression;
using System.Linq;
using Tender.Tool.Mysql;
using Tender.Tool.SqlServer;

namespace TestUnit1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            string connectionString = "server=192.168.90.173;Port=3306;Old Guids=true;password=teVUKe9pgraIwDXK;user=root;database=pcwp_outer;sslMode=None";

            string connectionString1 = "TrustServerCertificate=true;Server=192.168.100.6;Database=SRBG_Tender;User Id=sa;Password=asdw@122";

            DbContext  dbContext = new DbContext();
            dbContext.SqlConfigstr= connectionString1;

            DbContextmysql dbContextmysql = new DbContextmysql();
            dbContextmysql.SqlConfigstr = connectionString;
            var data = dbContext.Where("SELECT * FROM [dbo].[Ted_ZhaoBiaoBaoMinAudit] WHERE CompanyId='' AND State=8 ");
            foreach (DataRow item in data.Rows)
            {
                var data1 = item["SocialCode"].ToString();
                var da=   dbContextmysql.Where(data1);
                if (da != null)
                {
                    foreach (DataRow item1 in da.Rows)
                    {
                        if(item1["state"].ToString()=="2")
                        dbContext.Update($"UPDATE  Ted_ZhaoBiaoBaoMinAudit SET CompanyId='{item1["id"]}' WHERE BillId='{item["BillId"]}'");
                    }
                }
         

            }
            
            //using (MySqlConnection connection = new MySqlConnection(connectionString))
            //{
            //    try
            //    {
            //        connection.Open();

            //        string sql = "SELECT * FROM `outer_supplier` WHERE credit_code='91510121686324172W'";

            //        using (MySqlCommand command = new MySqlCommand(sql, connection))
            //        {
            //            using (MySqlDataAdapter adapter = new MySqlDataAdapter(command))
            //            {
            //                var dataTable = new System.Data.DataTable();
            //                adapter.Fill(dataTable);
            //                // 处理查询结果
            //                foreach (System.Data.DataRow row in dataTable.Rows)
            //                {
            //                    foreach (System.Data.DataColumn col in dataTable.Columns)
            //                    {
            //                        Console.Write($"{row[col]} ");
            //                    }
            //                    Console.WriteLine();
            //                }
            //            }
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //        Console.WriteLine("连接MySQL数据库时发生错误: " + ex.Message);
            //    }
            //}
        }

        [TestMethod]
        public void FLow()
        {

            string WORKID = "b06d11f3d91e-9424-3c48-772f-5773da1c";
            DbContext dbContext = new DbContext();
            dbContext.SqlConfigstr = "Data Source=192.168.100.150;Initial Catalog=SRBG_FlowEngin;User ID=sa;Password=asdw@123";//切换为流程引擎数据库
                var data1 = dbContext.Where($"SELECT * FROM [WORK]  WHERE WORKID='{WORKID}'");
            if (data1 != null && data1.Rows.Count > 0)
            {
                var data4 = dbContext.Where($"SELECT A.DISPID, B.TASKTYPE, C.UNITINDEX, C.UNITNAME, A.USERNAME, B.WORKID, A.STATE,B.STATE AS TASKSTATE,A.NOTIFYTIME, A.FINISHTIME, B.TASKID, E.PRIORTASKID, A.ISPASS " +
                    $"FROM USERDISP A WITH(NOLOCK) INNER JOIN TASK B WITH(NOLOCK) ON A.TASKID = B.TASKID INNER JOIN UNIT C WITH(NOLOCK) ON B.UNITID = C.UNITID INNER JOIN WORK D WITH(NOLOCK) ON B.WORKID = D.WORKID LEFT " +
                    $"JOIN TASKORDER E ON B.TASKID = E.NEXTTASKID WHERE D.STATE <> 2 AND " +
                    $"D.WORKID = '{WORKID}' ORDER BY E.PRIORTASKID");
                if (data4.AsEnumerable().Any())
                {
                    var itemdata = data4.AsEnumerable().ToList();
                    int indexi = 0;//记录第一个节点
                    string taskid = "";

                    for (int i = 0; i < itemdata.Count; i++)
                    {
                        if (itemdata[i]["TASKSTATE"].ToString() == "1")
                        {
                            if (indexi == 0) indexi = i;
                            for (int j = 0; j < itemdata.Count - indexi; j++)
                            {
                                if (itemdata[i + j]["TASKSTATE"].ToString() == "1" && itemdata[i + j]["TASKID"].ToString() != itemdata[i + j - 1]["TASKID"].ToString())
                                {
                                    taskid = itemdata[indexi]["TASKID"].ToString();
                                    goto outerLoop;
                                }
                            }
                        }
                    }
                outerLoop:
                    if (!string.IsNullOrEmpty(taskid))
                    {
                        bool yici = true;
                        var data2 = itemdata.Where(x => x["TASKID"].ToString() == taskid);
                        foreach (var item in data2)
                        {
                            if (item["TASKSTATE"].ToString() == "1")
                            {
                                int inde = dbContext.Update($"update USERDISP set STATE = 2,FINISHTIME = GETDATE(),ISPASS = 1  where DISPID = '{item["DISPID"]}'");
                                if (inde > 0 && yici)
                                {
                                    dbContext.Update($"update TASK set STATE = 2,FINISHTIME = GETDATE()  where TASKID = '{item["TASKID"]}'");
                                    yici = false;
                                }
                            }
                        }

                    }
                    else
                    {

                    }
                }




            }

        }

        [TestMethod]
        public void Testg()
        {
            CPMS.Tender.Service.TenderBasic tender = new CPMS.Tender.Service.TenderBasic();
           // tender.Test(CPMS.Tender.Model.Common.CommonEnum.BillType.GongKaiZhaoBiaoBaoMing, "00011255-3a6b-4976-bb9c-b6189e404d1a", false);
            tender.TestAudit(CPMS.Tender.Model.Common.CommonEnum.BillType.WuZiGongYing, "b0621f944b1d-bac1-6b42-238e-dfe30407",true);
        }

        [TestMethod]
        public void Zpitest()
        {
            string sourceDirectory = @"D:\Desktop\qqq\Tender";
            string zipFilePath = @"C:\qqq";

            try
            {
                ZipFile.CreateFromDirectory(sourceDirectory, zipFilePath);
               // ZipFile.ExtractToDirectory(sourceDirectory, zipFilePath);
            }
            catch (Exception ex)
            {

            }
        }
    }
}
